<?php
## DATETIME ####################################################################
// Converts datetime format to the given format string
// Returns datetime if no format is given
function datetime($format = NULL, $dt_or_ts = NULL, $offset = TRUE) {
	global $site_config;

	if ($format == NULL) $format = 'Y-m-d H:i:s';
	if ($dt_or_ts == NULL) $dt_or_ts = date('Y-m-d H:i:s', time());

	// If $dt_or_ts is datetime, make unix time from datetime
	if (!is_int($dt_or_ts)) {
		list($date, $time) = split(' ', $dt_or_ts);
		list($year, $month, $day) = split('-', $date);
		list($hour, $minute, $second) = split(':', $time);
		$timestamp = mktime((int)$hour, (int)$minute, (int)$second, (int)$month, (int)$day, (int)$year);
		$datestamp = date($format, $timestamp);
	} else {
		$timestamp = $dt_or_ts;
		$datestamp = date($format, $timestamp);
	}

	// Apply timezone offset
//	if ($offset) {
//		$dst = date('I', time()) * 3600;
//		if (!isset($site_config['timezone'])) $site_config['timezone'] = 0;
//		if (!isset($_SESSION['lf']['timezone'])) $_SESSION['lf']['timezone'] = 0;
//
//		if ($site_config['timezone'] != $_SESSION['lf']['timezone'])
//			$offset = ($_SESSION['lf']['timezone'] * 3600) - date('Z', time());
//		else
//			$offset = ($site_config['timezone'] * 3600) - date('Z', time());
//
//		$timestamp += $offset + $dst;
//		$datestamp = date($format, $timestamp);
//	}

	// Return datetime as current GMT time
	if ($format == NULL) return $datestamp;
	// Return timestamp as datetime
	elseif ($format == 'datetime') return date('Y-m-d H:i:s', $dt_or_ts);
	// Return datetime as timestamp
	elseif ($format == 'timestamp') return $timestamp;
	// Return datestamp formatted to $format
	else return $datestamp;
}


## MICROTIME ###################################################################
// Get a UNIX timestamp including microseconds
function micro_time() {
	list($usec, $sec) = explode(' ', microtime());
	return ((float) $usec + (float) $sec);
}

## SECONDS TO HUMAN ############################################################
// Convert seconds to human readable format
function seconds_to_human($seconds, $display = NULL) {
	if ($seconds < 0) return FALSE;

	$seconds = (int)$seconds;
	$s = ($seconds % 60).' '.($seconds == 1 ? 'second' : 'seconds');

	$minutes = (int)($seconds / 60);
	if ($minutes > 0) $m = ($minutes % 60).' '.($minutes == 1 ? 'minute' : 'minutes').' ';
	else $m = NULL;

	$hours = (int)($minutes / 60);
	if ($hours > 0) $h = ($hours % 60).' '.($hours == 1 ? 'hour' : 'hours').' ';
	else $h = NULL;

	$days = (int)($hours / 24);
	if ($days > 0) $d = ($days % 365).' '.($days == 1 ? 'day' : 'days').' ';
	else $d = NULL;

	$years = (int)($days / 365);
	if ($years > 0) $y = $years.' '.($years == 1 ? 'year' : 'years').' ';
	else $y = NULL;

	$string = $y.$d.$h.$m.$s;

	return $string;
}


## GET TIMEZONES ###############################################################
// Return an array of timezones
function get_timezones() {

	$timezones['-12'] = '-12';
	$timezones['-11'] = '-11';
	$timezones['-10'] = '-10';
	$timezones['-9.5'] = '-9.5';
	$timezones['-9'] = '-09';
	$timezones['-8.5'] = '-08.5';
	$timezones['-8'] = '-08 PST';
	$timezones['-7'] = '-07 MST';
	$timezones['-6'] = '-06 CST';
	$timezones['-5'] = '-05 EST';
	$timezones['-4'] = '-04 AST';
	$timezones['-3.5'] = '-3.5';
	$timezones['-3'] = '-03 ADT';
	$timezones['-2'] = '-02';
	$timezones['-1'] = '-01';
	$timezones['0'] = '00 GMT';
	$timezones['1'] = '+01 CET';
	$timezones['2'] = '+02';
	$timezones['3'] = '+03 ';
	$timezones['3.5'] = '+3.5';
	$timezones['4'] = '+04';
	$timezones['4.5'] = '+4.5';
	$timezones['5'] = '+05';
	$timezones['5.5'] = '+5.5';
	$timezones['6'] = '+6';
	$timezones['6.5'] = '+6.5';
	$timezones['7'] = '+07';
	$timezones['8'] = '+08';
	$timezones['9'] = '+09';
	$timezones['9.5'] = '+9.5';
	$timezones['10'] = '+10';
	$timezones['10.5'] = '+10.5';
	$timezones['11'] = '+11';
	$timezones['11.5'] = '+11.5';
	$timezones['12'] = '+12';
	$timezones['13'] = '+13';
	$timezones['14'] = '+14';

	return $timezones;
}
?>